package com.atguigu.daijia.map.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.map.service.MapService;
import com.atguigu.daijia.model.form.map.CalculateDrivingLineForm;
import com.atguigu.daijia.model.vo.map.DrivingLineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class MapServiceImpl implements MapService {

    //httpClient
    @Autowired
    private RestTemplate restTemplate;

    @Value("tencent.cloud.map")
    private String key;

    @Override
    public DrivingLineVo calculateDrivingLine(CalculateDrivingLineForm calculateDrivingLineForm) {
        //请求调用腾讯地图服务查路线信息
        //定义调用腾讯服务地址
        String url = "https://apis.map.qq.com/ws/direction/v1/driving/?from={from}&to={to}&key={key}";

        //封装传递参数
        Map<String,String> map = new HashMap();
        //开始位置
        // 经纬度：比如 北纬40 东京116
        map.put("from",calculateDrivingLineForm.getStartPointLatitude()+","+calculateDrivingLineForm.getStartPointLongitude());
        //结束位置
        map.put("to",calculateDrivingLineForm.getEndPointLatitude()+","+calculateDrivingLineForm.getEndPointLongitude());
        //key
        map.put("key",key);

        JSONObject result = restTemplate.getForObject(url, JSONObject.class, map);

        int status =  result.getIntValue("status");
        if (status != 0) {
            throw new GuiguException(ResultCodeEnum.MAP_FAIL);
        }
        //返回地图信息
        JSONObject routes = result.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
        //创建Vo对象
        DrivingLineVo drivingLineVo = DrivingLineVo
                .builder()
                .distance(routes.getBigDecimal("distance")
                        .divide(new BigDecimal(1000))
                        .setScale(2, RoundingMode.HALF_UP))
                .duration(routes.getBigDecimal("duration"))
                .polyline(routes.getJSONArray("polyline"))
                .build();
        return drivingLineVo;
    }
}
